import { NList, type ListProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import { getSlotsDom } from "../utils";
import type { BaseComponentType } from "../types";
export type ListFactoryConstructorType = {
  props?: ListProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  footerSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
};
export class ListFactory {
  private props: ListProps = {};
  private attrs: HTMLAttributes = {};
  private defaultSlot?: BaseComponentType = null;
  private footerSlot?: BaseComponentType = null;
  private headerSlot?: BaseComponentType = null;
  constructor(param?: ListFactoryConstructorType) {
    if (param?.props) this.setProps(param.props);
    if (param?.defaultSlot) this.setDefault(param.defaultSlot);
    if (param?.footerSlot) this.setFooter(param.footerSlot);
    if (param?.headerSlot) this.setHeader(param.headerSlot);
    if (param?.attrs) this.setAttrs(param.attrs);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: ListProps) {
    this.props = props;
    return this;
  }

  setDefault(defaultSlot: BaseComponentType) {
    this.defaultSlot = defaultSlot;
    return this;
  }

  setFooter(footerSlot: BaseComponentType) {
    this.footerSlot = footerSlot;
    return this;
  }

  setHeader(headerSlot: BaseComponentType) {
    this.headerSlot = headerSlot;
    return this;
  }

  create() {
    return (
      <NList {...this.props} {...this.attrs}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot as BaseComponentType)
              : null,
          footer:
            this.footerSlot != null
              ? () => getSlotsDom(this.footerSlot as BaseComponentType)
              : null,
          header:
            this.headerSlot != null
              ? () => getSlotsDom(this.headerSlot as BaseComponentType)
              : null,
        }}
      </NList>
    );
  }
}
